﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Data.Common;
using Mocola.BLL;
using Mocola.Model;

namespace Mocola.Net.Web.ajax
{
    public partial class AddPhase : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MembershipCreateStatus status = new MembershipCreateStatus();
            DbTransaction transaction = Databases.MocolaNet.BeginTransaction(IsolationLevel.ReadCommitted);

            try
            {
                string pPhaseName=string.Empty;
                string type = getRequest("ddlPhaseName");
                if (type.Equals("-1"))
                    pPhaseName = getRequest("phasename");        // 阶段名称
                else
                    pPhaseName = getRequest("ddlPhaseName");

                string pUid = getRequest("uid");                    // 父列表ID
                string pEid = getRequest("eid");                    // 项目ID
                int pType = Convert.ToInt32(getRequest("type")); // 等级类型

                int nPhaseID = Databases.MocolaNet.Insert(Moc_TreesInfo.Moc_Trees)
                    .AddColumn(Moc_TreesInfo.Moc_Trees.ParentID, Convert.ToInt32(pUid))
                    .AddColumn(Moc_TreesInfo.Moc_Trees.Name, pPhaseName)
                    .AddColumn(Moc_TreesInfo.Moc_Trees.URL, "#")
                    .AddColumn(Moc_TreesInfo.Moc_Trees.Title, pPhaseName)
                    .AddColumn(Moc_TreesInfo.Moc_Trees.Target, "")
                    .AddColumn(Moc_TreesInfo.Moc_Trees.Icon, "images/dtree/folder.gif")
                    .AddColumn(Moc_TreesInfo.Moc_Trees.IconOpen, "false")
                    .AddColumn(Moc_TreesInfo.Moc_Trees.IsOpen, "false")
                    .AddColumn(Moc_TreesInfo.Moc_Trees.Type, pType)
                    .AddColumn(Moc_TreesInfo.Moc_Trees.ExtId, Convert.ToInt32(pEid))
                    .SetTransaction(transaction)
                    .ExecuteReturnAutoIncrementID(Moc_TreesInfo.Moc_Trees.UniqueID);

                if (nPhaseID != -1)
                {
                    Response.Write(nPhaseID);
                    status = MembershipCreateStatus.Success;
                }
                else
                {
                    Response.Write(nPhaseID);
                    status = MembershipCreateStatus.ProviderError;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                status = MembershipCreateStatus.ProviderError;
            }
            finally
            {
                if (status == MembershipCreateStatus.Success)
                    transaction.Commit();
                else
                    transaction.Rollback();
                Databases.MocolaNet.CloseConnection(transaction);
            }
        }

        private string getRequest(string pReqID)
        {
            return string.IsNullOrEmpty(Request.Form[pReqID]) ? string.Empty : Request.Form[pReqID].ToString();
        }
    }
}
